/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.dess.base.apidoc;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Lists;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;
import java.util.Optional;

/**
 * @project-name:dess
 * @package-name:com.hyts.dess.apidoc
 * @author:LiBo/Alex
 * @create-date:2020-05-19 19:33
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */
@Configuration
@EnableSwagger2
public class ApiDocInterfaceConfiguration implements WebMvcConfigurer {


    /**
     * 这里省略 swagger api 基础配置信息
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    /**
     * 内部接口调用服务配置
     * @return
     */
    @Bean
    public Docket innerApi() {
        return new Docket(DocumentationType.SWAGGER_2).
                apiInfo(apiInfo(" 日常花销计费系统","日常花销计费系统相关内部服务接口")).
                globalOperationParameters(buildParameterList()).
                groupName("内部接口服务组").
                genericModelSubstitutes(DeferredResult.class).
                useDefaultResponseMessages(false).
                forCodeGeneration(true).select()
                // 选择匹配的控制器或URL路径用于生产 API document
                // 对所有api进行监控
                .apis(RequestHandlerSelectors.
                        withClassAnnotation(RestController.class)).
                        paths(PathSelectors.regex("api/inner/.*")).
                        build();
    }

    /**
     * 对外开放的接口服务机制
     * @return
     */
    @Bean
    public Docket openApi() {
       return new Docket(DocumentationType.SWAGGER_2).
                apiInfo(apiInfo(" 日常花销计费系统","日常花销计费系统相关外部服务接口")).
                globalOperationParameters(buildParameterList()).
                groupName("外部接口服务组").
                genericModelSubstitutes(DeferredResult.class).
                useDefaultResponseMessages(false).
                forCodeGeneration(true).select()
                // 选择匹配的控制器或URL路径用于生产 API document
                // 对所有api进行监控
                .apis(RequestHandlerSelectors.
                        withClassAnnotation(RestController.class)).
                        paths(PathSelectors.regex("api/open/.*")).
//                .apis(RequestHandlerSelectors.basePackage("com.hyts.coffice.web"))
//                .paths(PathSelectors.any())
                        build();
    }

    /**
     * api服务接口文档机制
     * @param name
     * @return
     */
    private ApiInfo apiInfo(String name,String description) {
        ApiInfo apiInfo =
                new ApiInfoBuilder()
                        .title(name)
                        .description(description)
                        .version("1.0.0")
                        .termsOfServiceUrl("http://localhost:8081/swagger-ui.html")
                        .contact(new Contact("LiBo/Alex", "http://www.cnblogs.com/liboware",
                                "liboware@gmail.com"))
                        .license("The Apache License, Version 2.0")
                        .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
                        .build();
        return apiInfo;
    }

    /**
     * 构建参数列表信息控制
     * @return
     */
    private List<Parameter> buildParameterList(){
        ParameterBuilder builder = new ParameterBuilder();
        Parameter parameter = builder
                //参数类型支持header, cookie, body, query etc
                .parameterType("header")
                //参数名
                .name("token")
                .description("请输入您的token令牌")
                //指定参数值的类型
                .modelRef(new ModelRef("string"))
                .required(false)
                .build();
        List<Parameter> parameters = Lists.newArrayList(parameter);
        return parameters;
    }


}
